//------------------------------------------------------------------------
//
//	CCGameBox is a game development library for iPhone and iPod Touch.
//
//	Licensed under the BSD license, see LICENSE in root for details.
// 
//	Copyright (c) 2009 James Hui (a.k.a. Dr.Watson)
// 
//	For latest updates, please visit http://www.cocoachina.com
//
//------------------------------------------------------------------------

#include "CCTypes.h"
#include "CCTexture.h"
#include "CCImage.h"

CCImage::CCImage(CCTexture *tex, float x, float y, float width, float height)
		:mTexture(tex), mX(x), mY(y), mWidth(width), mHeight(height)
{

	mAnchorX = 0.0f;
	mAnchorY = 0.0f;

	mColor.r = mColor.g = mColor.b = mColor.a = 1.0f;

	mHFlipped = false;
	mVFlipped = false;
	mOffsetX = -(float)width/2;
	mOffsetY = -(float)height/2; 

	SetTextureRect(x, y, width, height);

}


CCImage::CCImage(CCTexture *tex)
		:mTexture(tex)
{

	mX = 0;
	mY = 0;
	mWidth = tex->mImageWidth;
	mHeight = tex->mImageHeight;

	mAnchorX = 0.0f;
	mAnchorY = 0.0f;

	mColor.r = mColor.g = mColor.b = mColor.a = 1.0f;

	mHFlipped = false;
	mVFlipped = false;
	
    mOffsetX = -(float)mWidth/2;
	mOffsetY = -(float)mHeight/2;

	SetTextureRect(mX, mY, mWidth, mHeight);

}


void CCImage::SetTextureRect(float x, float y, float w, float h) 
{ 
	mX = x;
	mY = y;
	mWidth = w;
	mHeight = h;

	mTX0 = x/mTexture->mTextureWidth;
	mTX1 = (x+w)/mTexture->mTextureWidth;

	mTY0 = y/mTexture->mTextureHeight;
	mTY1 = (y+h)/mTexture->mTextureHeight;
}


void CCImage::GetTextureRect(float *x, float *y, float *w, float *h)  
{ 
	*x=mX; *y=mY; *w=mWidth; *h=mHeight; 
}


void CCImage::SetColor(float r, float g, float b, float a)
{
	mColor.r = r;
	mColor.g = g;
	mColor.b = b;
	mColor.a = a;
}


void CCImage::SetAnchor(float x, float y)
{
	mAnchorX = x;
	mAnchorY = y;
}


void CCImage::MakeCenterAsAnchor()
{
	mAnchorX = mWidth/2;
	mAnchorY = mHeight/2;
}

